import { createRouter, createWebHashHistory } from 'vue-router'
import Home from "../views/Home.vue";
import { post } from '../api/index'
import { getStorageValue, setStorageValue, tokenName } from '/src/utils'
import { table_list } from './table_list'
let sdps = [
    // 'sdp_workspace',
    'amis_workspace',
    // 'sdp_workspace_config',
    'amis_workspace_config',
    // 'sdp_project',
    'amis_project',
    'sdp_template',
    'sdp_sql',
    'amis_history',
]
table_list.sort(function compareFunction(p1, p2) {return p1.localeCompare(p2, 'zh') })
.filter( item => item.indexOf('sdp') == 0)
.map( item => {
    if (!sdps.find(ele => ele == item)
      && item != ''
    ) {
        sdps.push(item)
    }
})

let sdpChildren = []
for(let sdp in sdps) {
  sdp = sdps[sdp]
  sdpChildren.push({
    path: "/"+sdp,
    name: sdp,
    meta: {
        title: sdp
    },
    component: () => import ( `../views/sdp/${sdp}/index.vue`)
  })
}
export function getSdps() {
  return sdps;
}

const routes = [
    {
        path: '/',
        redirect: '/blank'
    }, {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            ...sdpChildren,
            {
                path: '/404',
                name: '404',
                meta: {
                    title: '找不到页面'
                },
                component: () => import (/* webpackChunkName: "404" */ '../views/404.vue')
            }, {
                path: '/403',
                name: '403',
                meta: {
                    title: '没有权限'
                },
                component: () => import (/* webpackChunkName: "403" */ '../views/403.vue')
            }, {
                path: '/blank',
                name: 'blank',
                meta: {
                    title: 'SDP'
                },
                component: () => import (/* webpackChunkName: "user" */ '../views/Blank.vue')
            }, {
                path: '/user',
                name: 'user',
                meta: {
                    title: '个人中心'
                },
                component: () => import (/* webpackChunkName: "user" */ '../views/User.vue')
            }
        ]
    }, {
        path: "/login",
        name: "Login",
        meta: {
            title: '登录'
        },
        component: () => import ( /* webpackChunkName: "login" */ "../views/Login.vue")
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

let lastRandom = false
router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | SDP`;
    let sessionid = getStorageValue(tokenName)
    sessionid = 'sessionid'
    // if (window.location.href.indexOf("?random=") >= 0 && window.location.href.indexOf("#") >= 0) {
    //     if (!lastRandom) {
    //         lastRandom = true;
    //     } else {
    //         let urls = window.location.href.split("?random=")
    //         let url = urls[0] + "#" + to.path
    //         window.location.replace(url)
    //         return
    //     }
    // }
    // const role = localStorage.getItem('ms_username');
    if (!sessionid && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        next()
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        // role === 'admin'
        //     ? next()
        //     : next('/403');
    } else {
        next();
    }
});

export default router;